<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="renderer" content="webkit|ie-comp|i、e-stand">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <title>公司设定</title>
</head>

<body>
  <div id="app" v-loading.fullscreen.lock="loading">
    <div ref="companySet" class="company-set clearfix" v-if="isPayUser!==null">
      <div class="basic-info">
        <div>
          <el-button type="primary" size="small" @click="setCompanySet">保存设置</el-button>
          <template v-if="isPayUser">
            <el-button type="primary" size="small" @click="showDeptInCountType">设置部门入库方式</el-button>
            <el-button type="primary" size="small" @click="showOtherOrderDept">设置订单外报工部门</el-button>
          </template>
        </div>
        <div v-if="companySetData !== null">
          <el-form label-width="auto" :model="companySetData">
            <template v-if="isPayUser">
              <el-form-item label="制单物料输入设置">
                <el-radio class="company-radio" v-model="companySetData.isUpdate" :label="1">只能选</el-radio>
                <el-radio class="company-radio" v-model="companySetData.isUpdate" :label="0">可输可选</el-radio>
              </el-form-item>
              <el-form-item label="设备订单来源">
                <el-radio class="company-radio" v-model="companySetData.machineGetOrderType" :label="0">车间周转量</el-radio>
                <el-radio class="company-radio" v-model="companySetData.machineGetOrderType" :label="1">订单列表</el-radio>
              </el-form-item>
              <el-form-item label="报工审批设置">
                <el-radio class="company-radio" v-model="companySetData.isApprovalReport" :label="1">需要审批</el-radio>
                <el-radio class="company-radio" v-model="companySetData.isApprovalReport" :label="0">不需要审批</el-radio>
              </el-form-item>
              <el-form-item label="报工不足数交接是否二次审核">
                <el-radio class="company-radio" v-model="companySetData.isNeedSecondApproval" :label="1">需要审批</el-radio>
                <el-radio class="company-radio" v-model="companySetData.isNeedSecondApproval" :label="0">不需要审核</el-radio>
              </el-form-item>
              <el-form-item label="报工是否可超数">
                <el-radio-group v-model="companySetData.isCanPassReport">
                  <el-radio class="company-radio" :label="0">否</el-radio>
                  <el-radio class="company-radio" :label="1">是(需二次审核)</el-radio>
                  <el-radio class="company-radio" :label="2">是(不需二次审核)</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="报工默认值">
                <el-radio class="company-radio" v-model="companySetData.reportDefaultValue" :label="1">投料数</el-radio>
                <el-radio class="company-radio" v-model="companySetData.reportDefaultValue" :label="0">0</el-radio>
              </el-form-item>
              <el-form-item label="结单后是否可报工">
                <el-radio class="company-radio" v-model="companySetData.afterFinishIsCanReport" :label="0">不可报工</el-radio>
                <el-radio class="company-radio" v-model="companySetData.afterFinishIsCanReport" :label="1">可报工</el-radio>
              </el-form-item>
              <el-form-item label="审核人是否修改报工单价">
                <el-radio class="company-radio" v-model="companySetData.approvalIsCanUpdateReportPrice" :label="1">可修改
                </el-radio>
                <el-radio class="company-radio" v-model="companySetData.approvalIsCanUpdateReportPrice" :label="0">不可修改
                </el-radio>
              </el-form-item>
              <el-form-item label="员工日报表计算工资方式">
                <el-radio class="company-radio" v-model="companySetData.dailyReckonType" :label="0">计数
                </el-radio>
                <el-radio class="company-radio" v-model="companySetData.dailyReckonType" :label="1">计时
                </el-radio>
              </el-form-item>
            </template>

            <!-- <el-form-item label="登录是否需要验证码">
              <el-radio class="company-radio" v-model="companySetData.isNeedCode" :label="1">需要</el-radio>
              <el-radio class="company-radio" v-model="companySetData.isNeedCode" :label="0">不需要</el-radio>
            </el-form-item> -->

            <template v-if="isPayUser">
              <el-form-item label="是否配料">
                <el-radio class="company-radio" v-model="companySetData.isMatching" :label="1">需要</el-radio>
                <el-radio class="company-radio" v-model="companySetData.isMatching" :label="0">不需要</el-radio>
              </el-form-item>
              <el-form-item label="申补单占比(%)">
                <el-input-number class="company-input" v-model="companySetData.startShare" :controls="false" :min="0" :max="100"></el-input-number>
                至
                <el-input-number class="company-input" v-model="companySetData.endShare" :controls="false" :min="0" :max="100"></el-input-number>
              </el-form-item>
              <el-form-item label="配料周期">
                <el-input-number class="company-input" v-model="companySetData.isNeedCode" :controls="false" :min="0" :max="365"></el-input-number>
                天
              </el-form-item>
              <el-form-item label="采购单打印是否需审核">
                <el-radio class="company-radio" v-model="companySetData.purchasePrintIsNeetApproval" :label="1">需要</el-radio>
                <el-radio class="company-radio" v-model="companySetData.purchasePrintIsNeetApproval" :label="0">不需要</el-radio>
              </el-form-item>
              <el-form-item label="采购单是否显示订单用料条数">
                <el-radio class="company-radio" v-model="companySetData.isShowYltsTotalOnPurchase" :label="1">显示</el-radio>
                <el-radio class="company-radio" v-model="companySetData.isShowYltsTotalOnPurchase" :label="0">不显示</el-radio>
              </el-form-item>
              <el-form-item label="被采购的物料是否需要审核">
                <el-radio class="company-radio" v-model="companySetData.isNeedApprovalByMaterial" :label="1">需要</el-radio>
                <el-radio class="company-radio" v-model="companySetData.isNeedApprovalByMaterial" :label="0">不需要</el-radio>
              </el-form-item>
            </template>

            <el-form-item label="仓库入库是否可修改单价单位">
              <el-radio class="company-radio" v-model="companySetData.canUpdatePriceAndUnit" :label="1">是</el-radio>
              <el-radio class="company-radio" v-model="companySetData.canUpdatePriceAndUnit" :label="0">否</el-radio>
            </el-form-item>
            <el-form-item label="是否可改物料入库仓位">
              <el-radio class="company-radio" v-model="companySetData.canUpdateMaterialPosition" :label="1">是</el-radio>
              <el-radio class="company-radio" v-model="companySetData.canUpdateMaterialPosition" :label="0">否</el-radio>
            </el-form-item>

            <template v-if="isPayUser">
              <el-form-item label="报工限制所有物料必须到齐">
                <el-radio class="company-radio" v-model="companySetData.isLimitMaterialNeedAll" :label="1">限制
                </el-radio>
                <el-radio class="company-radio" v-model="companySetData.isLimitMaterialNeedAll" :label="0">不限制
                </el-radio>
              </el-form-item>
              <el-form-item label="过滤采购拖期时间">
                <el-date-picker class="company-input" v-model="companySetData.notShowTardinessStartDate" type="date" placeholder="选择日期"
                  value-format="timestamp" :picker-options="{disabledDate:function(time){return dateLimit(time,'notShowTardinessStartDate');}}">
                </el-date-picker>
                至
                <el-date-picker class="company-input" v-model="companySetData.notShowTardinessEndDate" type="date" placeholder="选择日期"
                  value-format="timestamp" :picker-options="{disabledDate:function(time){return dateLimit(time,'notShowTardinessEndDate');}}">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="投料单物料单位相同是否可修改投料数">
                <el-radio class="company-radio" v-model="companySetData.isUpdateFeedOrderFeedTotal" :label="1">是</el-radio>
                <el-radio class="company-radio" v-model="companySetData.isUpdateFeedOrderFeedTotal" :label="0">否</el-radio>
              </el-form-item>
              <el-form-item label="投料单相同物料是否合并">
                <el-radio class="company-radio" v-model="companySetData.feedOrderMerge" :label="1">是</el-radio>
                <el-radio class="company-radio" v-model="companySetData.feedOrderMerge" :label="0">否</el-radio>
              </el-form-item>
              <el-form-item v-if="inOutReturnUserList!==null" label="出入库审核后退回操作人">
                <el-select filterable clearable style="width:100%" v-model="companySetData.inOutReturnUser">
                  <el-option v-for="inOutReturnUser in inOutReturnUserList" :key="inOutReturnUser.id" :value="inOutReturnUser.id"
                    :label="inOutReturnUser.name"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="收付款金额足够是否自动结算">
                <el-radio class="company-radio" v-model="companySetData.isAutoBalance" :label="1">是</el-radio>
                <el-radio class="company-radio" v-model="companySetData.isAutoBalance" :label="0">否</el-radio>
              </el-form-item>
              <!-- <el-form-item label="是否启用自动现金结算">
                <el-radio class="company-radio" v-model="companySetData.inWarehouseIsAutoBalance" :label="1">是</el-radio>
                <el-radio class="company-radio" v-model="companySetData.inWarehouseIsAutoBalance" :label="0">否</el-radio>
              </el-form-item> -->
              <el-form-item label="是否默认选中部门">
                <el-radio class="company-radio" v-model="companySetData.isSelectionDept" :label="1">是</el-radio>
                <el-radio class="company-radio" v-model="companySetData.isSelectionDept" :label="0">否</el-radio>
              </el-form-item>
              <el-form-item v-if="inOutReturnUserList!==null" label="bom清单开圈设定人员">
                <el-select filterable clearable style="width:100%" v-model="companySetData.operateBomKqSeter">
                  <el-option v-for="item in inOutReturnUserList" :key="item.id" :value="item.name"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="加班补卡写入补卡日志">
                <el-radio class="company-radio" v-model="companySetData.overTimeAddTblLog" :label="1">是</el-radio>
                <el-radio class="company-radio" v-model="companySetData.overTimeAddTblLog" :label="0">否</el-radio>
              </el-form-item>
              <!-- operateBomKqSeter -->
            </template>
          </el-form>
          <div class="other-order-dept" v-if="isPayUser">
            <div>订单外报工部门:</div>
            <div class="deptList">
              <template v-for="(dept,index) in otherOrderDept">
                <span>{{dept.deptName}}</span>
              </template>
            </div>
          </div>
        </div>
      </div>
      <div class="material-setting-div">
        <material-setting v-if="materialList!==null" :materials="materialList"></material-setting>
      </div>
    </div>

    <el-dialog v-if="isPayUser" title="设置部门入库方式" :close-on-press-escape="false" :close-on-click-modal="false" :visible.sync="isShowSetDept" top="5vh">
      <div style="height:calc(80vh - 54px);overflow:auto">
        <set-dept-in-count-type v-if="isShowSetDept" :dept-list="deptList" @cancel="deptCancel">
        </set-dept-in-count-type>
      </div>
    </el-dialog>

    <el-dialog v-if="isPayUser" title="添加订单外报工部门" :close-on-press-escape="false" :close-on-click-modal="false" :visible.sync="isShowOtherOrderDept"
      top="5vh">
      <div style="height:calc(80vh - 54px);overflow:auto">
        <set-other-order-dept v-if="isShowOtherOrderDept" @cancel="otherOrderCancel">
        </set-other-order-dept>
      </div>
    </el-dialog>
  </div>
</body>

<script src="myJs/importFile.js"></script>
<script>
  importFile([
    "/companySet/css/materialSetting.css"
  ], [
    "/vueSystem/js/checkVaildUtil.js",
    "/vueSystem/models/baseModel.js",
    "/companySet/models/companySetModel.js",
    "/companySet/models/materialModel.js",
    "/companySet/setDeptInCountType.js",
    "/companySet/setOtherOrderDept.js",
    "/companySet/materialSetting.js"
  ]);

</script>
<script>
  var app = new Vue({
    el: '#app',
    data: function () {
      return {
        loading: false,
        isPayUser: null,
        user: null,
        moment: moment,
        companySetData: null,
        deptList: [],
        otherOrderDept: [],
        productionDeptList: [],
        inOutReturnUserList: null,
        materialList: null,
        isShowForm: false,
        isShowSetDept: false,
        isShowOtherOrderDept: false,
        isApprovalReport: null
      };
    },
    created: function () {
      this.loading = true;
      var self = this;
      Promise.all([
        this.setUser(),
        this.queryData(),
        this.setOtherOrderDept(),
        this.setMaterialList()
      ]).then(function () {
        self.loading = false;
      }).catch(function () {
        self.loading = false;
      });
    },
    methods: {
      setUser: function () {
        var self = this;
        return SysUtil.getUser().then(function (user) {
          var isPayUser = user.companyInfo.status === 1;
          self.isPayUser = isPayUser;
          self.user = user;

          if (user.isManager === 1) {
            return self.setInOutReturnUserList();
          }
          return user;
        });
      },

      setMaterialList: function () {
        var self = this;
        return ajaxRequest('company/getMaterialConfig').then(function (materialList) {
          materialList = _.map(materialList, function (material) {
            material = new MaterialModel(material);
            return material;
          });
          self.materialList = materialList;
          return materialList;
        });
      },

      setInOutReturnUserList: function () {
        var self = this;
        return ajaxRequest('dingTalk/askForApprover').then(function (personList) {
          personList = _.map(personList, function (person) {
            return {
              id: String(person.id),
              name: person.personName
            };
          });
          personList = _.uniqBy(personList, 'id');
          self.inOutReturnUserList = personList;
        });
      },

      queryData: function (isSearch) {
        var self = this;
        return ajaxRequest("company/getCompany").then(function (setting) {
          var companySetData = new CompanySet(setting);
          self.isApprovalReport = companySetData.isApprovalReport;
          self.companySetData = companySetData;
          return companySetData;
        });
      },

      setCompanySet: function () {
        var companySet = this.companySetData;
        if (!companySet.checkValid()) {
          return;
        }

        if (companySet.startShare > companySet.endShare) {
          this.$alert("申补单最小占比不能大于最大占比", "信息");
          return;
        }

        var postData = companySet.getObject();
        var isApprovalReport = companySet.isApprovalReport;
        if (this.isApprovalReport === 0 && isApprovalReport === 1) {
          var self = this;
          this.$confirm('是否将此前报工记录全部审核？', '提示', {
            cancelButtonText: "否",
            confirmButtonText: "是",
            showClose: false,
            closeOnClickModal: false,
            closeOnPressEscape: false
          }).then(function () {
            postData.approvalAllReport = true;
            self.toSaveSetting(postData);
          }).catch(function () {
            self.toSaveSetting(postData);
          });
          return;
        }
        this.toSaveSetting(postData);
      },

      toSaveSetting: function (postData) {
        var self = this;
        this.loading = true;
        ajaxRequest("company/setCompany", postData).then(function (res) {
          self.$alert(res, "信息");
          var user = self.user;
          var companyInfo = user.companyInfo;
          _.assign(companyInfo, postData);
          user.companyInfo = companyInfo;
          window.sessionStorage.setItem('user', JSON.stringify(user));

          self.isApprovalReport = postData.isApprovalReport;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      showDeptInCountType: function () {
        var self = this;
        this.loading = true;
        ajaxRequest("basicData/getProductionDepartment").then(function (res) {
          self.deptList = res;
          self.loading = false;
          self.isShowSetDept = true;
        }).catch(function () {
          self.loading = false;
        });
      },
      deptCancel: function () {
        this.isShowSetDept = false;
      },
      showOtherOrderDept: function () {
        this.isShowOtherOrderDept = true;
      },
      setOtherOrderDept: function () {
        var self = this;
        return ajaxRequest("order/getOtherDept").then(function (otherOrderDept) {
          self.otherOrderDept = otherOrderDept;
        });
      },
      otherOrderCancel: function () {
        this.isShowOtherOrderDept = false;

        this.loading = true;
        var self = this;
        this.setOtherOrderDept().then(function () {
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },
      dateLimit: function (time, field) {
        var companySetData = this.companySetData;
        var date;
        var fun;
        if (field === 'notShowTardinessStartDate') {
          date = companySetData.notShowTardinessEndDate;
          fun = 'isAfter';
        } else if (field === 'notShowTardinessEndDate') {
          date = companySetData.notShowTardinessStartDate;
          fun = 'isBefore';
        }
        if (date === undefined || fun === undefined) {
          return false;
        }
        if (moment(time)[fun](date, 'day')) {
          return true;
        }
        return false;
      }
    },
    components: {
      CompanySet: CompanySet,
      SetDeptInCountType: SetDeptInCountType,
      SetOtherOrderDept: SetOtherOrderDept,
      MaterialSetting: MaterialSetting
    }
  });

</script>

<style>
  .company-set {
    padding: 0 20px;
    height: 100%;
    min-width: 600px;
  }

  .company-set>div {
    float: left;
    overflow: auto;
  }

  .company-set .basic-info {
    width: 620px;
    padding: 10px 0 5px;
    height: calc(100% - 15px);
  }

  .company-set .material-setting-div {
    width: calc(100% - 630px);
    min-width: 300px;
    padding-left: 10px;
    height: 100%;
  }

  .company-set .el-form-item {
    margin-bottom: 5px;
  }

  .company-set .basic-info .company-radio.el-radio {
    min-width: 80px;
    margin-right: 0;
  }

  .company-set .basic-info .el-radio+.el-radio {
    margin-left: 10px;
  }

  .company-set .basic-info .company-input {
    width: 140px;
  }

  .company-set .basic-info .other-order-dept .deptList {
    margin-top: 5px;
    color: #606266;
  }

  .company-set .basic-info .other-order-dept .deptList>span+span {
    margin-left: 10px;
  }

  @media screen and (max-width:990px) {
    .company-set>div {
      float: none !important;
      width: 100% !important;
      min-width: 100% !important;
    }

    .company-set>div.basic-info {
      height: auto;
    }

    .company-set>div.material-setting-div {
      padding-left: 0;
      border-top: 1px solid #cccccc;
    }
  }

</style>

</html>
